{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c932c517",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlwings as xw\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4408e153",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = xw.App(visible=False, add_book=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d30b0429",
   "metadata": {},
   "source": [
    "### 读取Excel数据到Pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1cb1c738",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook = app.books.open(\"本月份上衣销量数据.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d3a825f",
   "metadata": {},
   "outputs": [],
   "source": [
    "sheet = workbook.sheets[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "424f8a25",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = sheet.range(\"A1\").options(pd.DataFrame, expand='table').value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "45c43339",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>类型</th>\n",
       "      <th>销量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-07-01</th>\n",
       "      <td>上衣</td>\n",
       "      <td>691.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-02</th>\n",
       "      <td>上衣</td>\n",
       "      <td>121.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-03</th>\n",
       "      <td>上衣</td>\n",
       "      <td>118.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-04</th>\n",
       "      <td>上衣</td>\n",
       "      <td>879.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-05</th>\n",
       "      <td>上衣</td>\n",
       "      <td>470.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            类型     销量\n",
       "日期                   \n",
       "2021-07-01  上衣  691.0\n",
       "2021-07-02  上衣  121.0\n",
       "2021-07-03  上衣  118.0\n",
       "2021-07-04  上衣  879.0\n",
       "2021-07-05  上衣  470.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "477cc08b",
   "metadata": {},
   "source": [
    "### 实现画图，添加到Sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9f3fead2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2021-07-01    691.0\n",
       "2021-07-02    121.0\n",
       "2021-07-03    118.0\n",
       "2021-07-04    879.0\n",
       "2021-07-05    470.0\n",
       "Name: 销量, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"销量\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7071563c",
   "metadata": {},
   "outputs": [],
   "source": [
    "figure = plt.figure(figsize=(12, 6), dpi=100)\n",
    "\n",
    "plt.plot(df[\"销量\"])\n",
    "\n",
    "sheet.pictures.add(figure, \n",
    "                   name='折线图', \n",
    "                   update=True,\n",
    "                   left=sheet.range('E3').left, \n",
    "                   top=sheet.range('E3').top)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8abee91c",
   "metadata": {},
   "source": [
    "### 退出Xlwings程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0ff75ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook.save()\n",
    "workbook.close()\n",
    "app.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b376ea2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
